#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 19-6-10 下午8:22
# @Author  : Tom
# @Site    : 
# @File    : crons.py
# @Software: PyCharm

from django.shortcuts import render, redirect
from .models import ContentCategory, Content
from meiduo_mall.utils.categories import get_categories
from django.conf import settings
import os


def generate_static_index_html():
    categories = get_categories()
    # 广告数据
    contents = {}
    # 获取广告内容类型对象列表
    content_category = ContentCategory.objects.all()
    for cat in content_category:
        # 并且一个字典：键是cat.key 值是列表对象
        contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence')
    # 渲染模板
    context = {
        "categories": categories,
        'contents': contents,
    }
    response = render(None, 'index.html', context)
    htm_str = response.content.decode()
    with open(os.path.join(settings.BASE_DIR, 'static/index.html'), 'w') as file:
        file.write(htm_str)
